﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace XQ.Framework.DateTimeHelper
{
    public class QuarterHelper
    {
        /// <summary>
        /// 获取指定年份季度的第一天
        /// </summary>
        /// <param name="year">年份</param>
        /// <param name="quarter">季度(1-4)</param>
        public static DateTime GetFirstDayOfQuarter(int year, int quarter)
        {
            if (quarter < 1 || quarter > 4)
                throw new ArgumentOutOfRangeException(nameof(quarter), "季度参数必须为1-4");

            int month = (quarter - 1) * 3 + 1;
            return new DateTime(year, month, 1);
        }

        /// <summary>
        /// 获取指定年份季度的最后一天
        /// </summary>
        /// <param name="year">年份</param>
        /// <param name="quarter">季度(1-4)</param>
        public static DateTime GetLastDayOfQuarter(int year, int quarter)
        {
            if (quarter < 1 || quarter > 4)
                throw new ArgumentOutOfRangeException(nameof(quarter), "季度参数必须为1-4");

            int month = quarter * 3;
            int day = DateTime.DaysInMonth(year, month);
            return new DateTime(year, month, day);
        }

        /// <summary>
        /// 获取当前日期所在季度的第一天
        /// </summary>
        public static DateTime GetFirstDayOfCurrentQuarter()
        {
            DateTime now = DateTime.Now;
            int quarter = (now.Month - 1) / 3 + 1;
            return GetFirstDayOfQuarter(now.Year, quarter);
        }

        /// <summary>
        /// 获取当前日期所在季度的最后一天
        /// </summary>
        public static DateTime GetLastDayOfCurrentQuarter()
        {
            DateTime now = DateTime.Now;
            int quarter = (now.Month - 1) / 3 + 1;
            return GetLastDayOfQuarter(now.Year, quarter);
        }

        /// <summary>
        /// 指定月份的第一天
        /// </summary>
        /// <param name="year"></param>
        /// <param name="month"></param>
        /// <returns></returns>
        public static DateTime GetFirstDayOfMonth(int year, int month)
        { 
            return new DateTime(year, month, 1);
        }

        /// <summary>
        /// 指定月份的第后一天
        /// </summary>
        /// <param name="year"></param>
        /// <param name="month"></param>
        /// <returns></returns>
        public static DateTime GetLastDayOfMonth(int year, int month)
        {
            return new DateTime(year, month, DateTime.DaysInMonth(year, month)); ;
        }
    }
}
